package com.woniuxy.woniuhis.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.woniuhis.entity.Examination;
import com.woniuxy.woniuhis.entity.ExaminationItem;
import com.woniuxy.woniuhis.entity.ExaminationName;
import com.woniuxy.woniuhis.entity.ExaminationResult;
import com.woniuxy.woniuhis.entity.MediItem;
import com.woniuxy.woniuhis.entity.Medicine;
import com.woniuxy.woniuhis.entity.RecipeMedi;
import com.woniuxy.woniuhis.exception.ExaminationServiceException;
import com.woniuxy.woniuhis.mapper.ExaminationItemMapper;
import com.woniuxy.woniuhis.mapper.ExaminationMapper;
import com.woniuxy.woniuhis.mapper.ExaminationNameMapper;
import com.woniuxy.woniuhis.mapper.ExaminationResultMapper;
import com.woniuxy.woniuhis.mapper.MedicineMapper;
import com.woniuxy.woniuhis.mapper.RecipeMediMapper;
import com.woniuxy.woniuhis.service.ExaminationService;
import com.woniuxy.woniuhis.util.ResponseJson;
import com.woniuxy.woniuhis.util.UUIDUtil;

@Service
public class ExaminationServiceImpl implements ExaminationService {
	@Autowired
	ExaminationMapper examinationMapper;

	@Autowired
	ExaminationItemMapper examinationItemMapper;

	@Autowired
	ExaminationNameMapper examinationNameMapper;

	@Autowired
	ExaminationResultMapper examinationResultMapper;

	@Autowired
	MedicineMapper medicineMapper;
	
	@Autowired
	RecipeMediMapper recipeMediMapper;

	/**
	 * 开检查单
	 */
	@Override
	@Transactional
	public void addExamination(Examination ex) {
		try {
			// 添加检查单
			examinationMapper.insertExamination(ex);
			// 添加属于该检查单的检查条目
			/*
			 * if(true) { throw new RuntimeException("断网了"); }
			 */
			for (ExaminationItem exItem : ex.getExaminationItems()) {
				examinationItemMapper.insertExaminationItem(exItem);
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new ExaminationServiceException("系统维护中");
		}

	}

	/**
	 * 查询编号指定检查单
	 */
	@Override
	public Examination findExaminationById(String exId) {
		try {
			// 查检查单
			Examination examination = examinationMapper.selectExaminationById(exId);
			// 查检查单的检查条目
			List<ExaminationItem> examinationItems = examinationItemMapper
					.selectAllExaminationItemsByExaminationId(exId);
			// 每一个检查条目对应的检查项目
			for (ExaminationItem item : examinationItems) {
				item.setExaminationName(examinationNameMapper.selectExaminationNameById(item.getExamNameId()));
			}
			examination.setExaminationItems(examinationItems);
			return examination;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ExaminationServiceException(e);
		}
	}

	/**
	 * 查询所有的检查单，满足条件的检查单
	 */
	@Override
	public PageInfo<Examination> findAllExaminations(Examination ex, int pageIndex, int pageSize) {
		PageHelper.startPage(pageIndex, pageSize);
		try {
			// 符合条件的检查单
			List<Examination> exams = examinationMapper.selectAllExaminations(ex);
			for (Examination exam : exams) {
				// 查检查单的检查条目
				List<ExaminationItem> examinationItems = examinationItemMapper
						.selectAllExaminationItemsByExaminationId(exam.getExaminationId());
				// 每一个检查条目对应的检查项目
				for (ExaminationItem item : examinationItems) {
					item.setExaminationName(examinationNameMapper.selectExaminationNameById(item.getExamNameId()));
				}
				exam.setExaminationItems(examinationItems);
			}
			PageInfo<Examination> pageInfo = new PageInfo<Examination>(exams);
			return pageInfo;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ExaminationServiceException(e);
		}
	}

	/**
	 * 查询所有检查名称
	 */
	@Override
	public ResponseJson<ExaminationName> findAllExaminationNames() {
		try {
			List<ExaminationName> allExaminationName = examinationNameMapper
					.selectAllExaminationName(new ExaminationName());
			// 封装成layui能识别的数据结构
			ResponseJson<ExaminationName> responseJson = new ResponseJson<ExaminationName>();
			responseJson.setCode(0);
			responseJson.setCount(allExaminationName.size());
			responseJson.setData(allExaminationName);
			responseJson.setMsg("");
			return responseJson;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ExaminationServiceException("系统维护中");
		}
	}

	@Override
	public ResponseJson<ExaminationName> findAllExaminationNamesByCondition(ExaminationName examinationName) {
		try {
			List<ExaminationName> names = examinationNameMapper.selectAllExaminationName(examinationName);
			// 封装成layui能识别的数据结构
			ResponseJson<ExaminationName> responseJson = new ResponseJson<ExaminationName>();
			responseJson.setCode(0);
			responseJson.setCount(names.size());
			responseJson.setData(names);
			responseJson.setMsg("");
			return responseJson;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ExaminationServiceException("系统维护中");
		}
	}

	@Override
	@Transactional
	public String addExamination(String examNameIds, String outpatientId) {
		try {
			// 生成检查单的id，门诊的检查id前面要有out
			String examinationId = "out"+UUIDUtil.uuid32();
			// 检查单的总金额
			Double total = 0.0;
			// 生成多条检查项
			String[] examNames = examNameIds.split(",");
			for (String examName : examNames) {
				ExaminationName name = examinationNameMapper.selectExaminationNameById(examName);
				// 创建一个检查条目并插入到数据库
				ExaminationItem exItem = new ExaminationItem(UUIDUtil.uuid32(), examinationId, examName, 1,
						name.getExamNamePrice(), false);
				examinationItemMapper.insertExaminationItem(exItem);
				// 合计检查单总金额
				total += name.getExamNamePrice();
			}
			// 生成检查单并插入到数据库
			Examination examination = new Examination(examinationId, total, new Date(), false, null, outpatientId);
			examinationMapper.insertExamination(examination);
			return examinationId;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ExaminationServiceException("系统维护中");
		}
	}

	/**
	 * 根据Examination的id查询该检查单的检查项的所有检查结果
	 */
	@Override
	public ResponseJson<ExaminationResult> findAllExamResultByExamId(String examinationId) {
		try {
			// 根据检查单号查询所有检查项
			List<ExaminationItem> examinationItems = examinationItemMapper
					.selectAllExaminationItemsByExaminationId(examinationId);
			// 创建一个list存检查结果
			List<ExaminationResult> results = new ArrayList<ExaminationResult>();
			// 根据检查项的id查询所有对应的检查结果
			for (ExaminationItem item : examinationItems) {
				ExaminationResult result = examinationResultMapper
						.selectExaminationResultByExamItemId(item.getExamItemId());
				results.add(result);
			}
			// 封装成layui能识别的数据结构
			ResponseJson<ExaminationResult> responseJson = new ResponseJson<ExaminationResult>();
			responseJson.setCode(0);
			responseJson.setCount(results.size());
			responseJson.setData(results);
			responseJson.setMsg("");
			return responseJson;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ExaminationServiceException("系统维护中");
		}
	}

	/**
	 * 由于返回值不一样，自己写一个查所有药物的方法
	 */
	@Override
	public ResponseJson<Medicine> findAllMedicine() {
		try {
			List<Medicine> allMedicine = medicineMapper.selectAllMedicine();
			// 封装成layui能识别的数据结构
			ResponseJson<Medicine> responseJson = new ResponseJson<Medicine>();
			responseJson.setCode(0);
			responseJson.setCount(allMedicine.size());
			responseJson.setData(allMedicine);
			responseJson.setMsg("");
			return responseJson;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ExaminationServiceException("系统维护中");
		}
	}

	@Override
	public ResponseJson<Medicine> findAllMedicineByMedicineName(Medicine medicine) {
		try {
			List<Medicine> medicines = medicineMapper.selectMediByMediName(medicine);
			// 封装成layui能识别的数据结构
			ResponseJson<Medicine> responseJson = new ResponseJson<Medicine>();
			responseJson.setCode(0);
			responseJson.setCount(medicines.size());
			responseJson.setData(medicines);
			responseJson.setMsg("");
			return responseJson;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ExaminationServiceException("系统维护中");
		}
	}

	@Override
	@Transactional
	public String addRecipeMedi(String mediIds, String outpatientId) {
		try {
			// 生成处方药单的id,门诊的处方药单id前面要有out
			String recipeId = "out"+ UUIDUtil.uuid32();
			//创建一个处方药单为recipeId的处方药单实体
			RecipeMedi recipeMedi= new RecipeMedi();
			recipeMedi.setRecipeMediId(recipeId);
			// 处方药单的总金额
			Double total = 0.0;
			// 生成多条处方药项
			String[] medis = mediIds.split(",");
			//System.out.println(medis[0]);
			for (String mediId : medis) { 
				//根据药物id查找药物
				 Medicine name = medicineMapper.selectMediById(mediId); 
				 // 创建一个处方条目并插入到数据库
				 //创建一个存入的药物
				 Medicine medicine = new Medicine();
				 medicine.setMediId(mediId);
				 //找到该条目对应的药物
				 Medicine selectMediById = medicineMapper.selectMediById(mediId);
				 MediItem item = new MediItem(UUIDUtil.uuid32(), medicine, 1, selectMediById.getMediPrice(), recipeMedi, false);
				 total += selectMediById.getMediPrice();
			}
			recipeMedi.setSourceId(outpatientId);
			recipeMedi.setRecipeMediDate(new Date());
			recipeMedi.setPayStatus(false);
			recipeMedi.setRecipeMediTotal(total);
			//将处方药单存入数据库
			recipeMediMapper.insertRecipeMedi(recipeMedi);
			return recipeId;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ExaminationServiceException("系统维护中");
		}
	}
}
